<?php

namespace app\common\model;

use app\common\model\Base;
use app\common\model\User;
use think\Model;

class OrderListSend extends Base
{

    // 设置当前模型对应的完整数据表名称
    protected $table = 'order_list_send';

    /**
     * 获取列表
     */
    public static function getList($where = [], $field = '*', $order = '', $pageSize = "15")
    {
        $list = self::where($where)
            ->field($field)
            ->order($order)
            ->paginate(['list_rows' => $pageSize, 'query' => request()->param()]);
        $page = $list->render();
        $data['list'] = $list->toArray()['data'];
        $data['count'] = $list->total();
        $data['last_page'] = $list->toArray()['last_page'];
        $data['page'] = $page;
        return $data;
    }

    /**
     * 获取列表  不分页
     */
    public static function getAllList($where = [], $field = '*', $order = '', $limit = '0')
    {
        $data = self::where($where)
            ->field($field)
            ->order($order)
            ->limit($limit)
            ->select();
        return $data;
    }

    /**
     * 获取单条数据
     */
    public static function getInfo($where = [], $field = '*')
    {
        $data = self::where($where)
            ->field($field)
            ->find();
        return $data;
    }

    public function getUserInfoAttr($value, $data)
    {
        $user_info = User::field('nickname,headimg,mobile')->where(['id' => $data['user_id']])->find();
        return $user_info;
    }


    public function getStatusNameAttr($value, $data)
    {
        $status_name = '未知';
        if ($data['status'] == 1) {
            $status_name = '待发货';
        } elseif ($data['status'] == 2) {
            $status_name = '待收货';
        } elseif ($data['status'] == 3) {
            $status_name = '已完成';
        } elseif ($data['status'] == 4) {
            $status_name = '已取消';
        }
        return $status_name;
    }

    public function getOrderListAttr($value, $data)
    {
        $data = OrderList::field('goodslist_title,goodslist_imgurl,shang_id,goodslist_money,count(id) as prize_num')
            ->append(['shang_title'])
            ->where(['send_num' => $data['send_num']])
            ->withAttr('goodslist_imgurl', function ($value, $data) {
                return imageUrl($value);
            })
            ->group("prize_code")
            ->select();
        return $data;
    }


}